/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.ui.basic;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.client.util.Answer;
import com.sinai.mshab.client.ui.FormComponent;
import com.sinai.mshab.client.ui.aggregate.DataReceiver;


public abstract class QuestionComponent extends BasicFormComponent {

	protected void initializeWidget(String dbfieldName, String tableName,
			String description, String quantifier, String error_msg,
			boolean horizontal) {
		
	}
	
	public void sendAnswer(DataReceiver receiver) {
		receiver.sendData(getAnswer());
	}

	public Answer getAnswer() {
		Answer answer = new Answer();
		answer.add(tableName, dbfieldName, ((TextBox) widget).getText());
		answer.add(hiddenFields);
		return answer;
	}

	/**
	 * When to focus is lost validate the field.
	 */
	public void onFocus(Widget sender) {
		String name = ((TextBox) widget).getStyleName();
		((TextBox) widget).setStyleName(name + "_focus");
	}

	/**
	 * When to focus is lost validate the field.
	 */
	public void onLostFocus(Widget sender) {

		String name = ((TextBox) widget).getStyleName();
		if (name.indexOf("_focus") != -1) {
			((TextBox) widget).setStyleName(name.substring(0, name.length()
					- "_focus".length()));
		}

		validate();
	}

	/**
	 * Sets the text of the textbox to 'message'
	 * 
	 * @param message
	 *            The text that will go to the text field.
	 */
	public void setText(String message) {
		((TextBox) widget).setText(message);
	}
	
	/** 
	 * Ask the validator to perform validation on this widget's 
	 * getAnswer()
	 * If validator does not aprove, display error message.
	 */
	public boolean validate() {
		
		boolean success;
		
		// do no validation if our validator is not set.
		if (validator == null) {
			success = true;
		} else {
		
			// remember if we passed the validation
			success = validator.validate(this);
			
			// if validation failed, error occured and error label should be
			// displayed
			error_label.setVisible(!success);
		}
		
		return success;
	}
	
	/**
	 * @see FormComponent#isEmpty()
	 */
	public boolean isEmpty() {
		return getText().equals("");
	}
	
	/**
	 * Returns a String representation of the user entered information.
	 * 
	 * @return the user entered information.
	 */
	public abstract String getText();

	public void setEnabled(boolean value) {
		((TextArea) widget).setEnabled(value);
	}

	/**
	 * Set's the textbox's css style.
	 * 
	 * @param style
	 *            the new css style name
	 */
	public void setInnerStyleName(String style) {
		((TextArea) widget).setStyleName(style);
	}

	public HorizontalPanel toReport() {
		return super.toPrint(getText().trim());
	}
}
